﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;

namespace Personify.Modules.FlickrBadge
{
    public partial class Settings : ModuleSettingsBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlSource.Attributes.Add("onchange", "ShowHideOptions()");
                ddlSource.Attributes.Add("onkeyup", "ShowHideOptions()");
            }
        }

        public override void LoadSettings()
        {
            try
            {
                if (Page.IsPostBack == false)
                {
                    // Load items count drop down
                    ddlImageCount.Items.Clear();
                    for (int i = 1; i <= 10; i++)
                    {
                        ddlImageCount.Items.Add(new ListItem(i.ToString()));
                    }

                    // Load all module settings on the page
                    int imageCount;
                    if (int.TryParse((string)TabModuleSettings["ImageCount"], out imageCount))
                    {
                        imageCount = Math.Max(1, imageCount);
                        imageCount = Math.Min(imageCount, 10);      // 10 is max
                        ddlImageCount.SelectedValue = imageCount.ToString();
                    }

                    if (TabModuleSettings["Display"] != null)
                    {
                        ddlDisplay.SelectedValue = (string)TabModuleSettings["Display"];
                    }

                    if (TabModuleSettings["Source"] != null)
                    {
                        ddlSource.SelectedValue = (string)TabModuleSettings["Source"];
                    }

                    if (TabModuleSettings["FlickrId"] != null)
                    {
                        tbFlickrId.Text = (string)TabModuleSettings["FlickrId"];
                    }

                    if (TabModuleSettings["Tag"] != null)
                    {
                        tbTag.Text = (string)TabModuleSettings["Tag"];
                    }

                    if ((string)TabModuleSettings["OpenInFlickr"] == "1")
                    {
                        rbOpenInFlickr.Checked = true;
                    }
                    else
                    {
                        rbRefreshOnPage.Checked = true;
                    }
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        public override void UpdateSettings()
        {
            try
            {
                // Save all the module settings on the page
                ModuleController objModules = new ModuleController();
                objModules.UpdateTabModuleSetting(TabModuleId, "ImageCount", ddlImageCount.SelectedValue);
                objModules.UpdateTabModuleSetting(TabModuleId, "Display", ddlDisplay.SelectedValue);
                objModules.UpdateTabModuleSetting(TabModuleId, "Source", ddlSource.SelectedValue);
                objModules.UpdateTabModuleSetting(TabModuleId, "FlickrId", tbFlickrId.Text);
                objModules.UpdateTabModuleSetting(TabModuleId, "Tag", tbTag.Text);

                if (rbRefreshOnPage.Checked)
                {
                    objModules.UpdateTabModuleSetting(TabModuleId, "OpenInFlickr", "0");
                }
                else
                {
                    objModules.UpdateTabModuleSetting(TabModuleId, "OpenInFlickr", "1");
                }

                ModuleController.SynchronizeModule(ModuleId);
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }
    }
}